package com.baor.service.impl;

import com.baor.dao.MenuDao;
import com.baor.dao.RoleDao;
import com.baor.domain.MenuVo;
import com.baor.service.MenuService;
import com.baor.utils.AjaxResult;
import com.baor.utils.RedisUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RoleDao roleDao;
    @Override
    public AjaxResult<?> menuList(int roleId) {
        int roleStatus = roleDao.roleStatus(roleId);
        if(roleStatus == 0){
            List<MenuVo> menuVos = this.menuDao.roleMenuList(roleId);
//            System.out.println(menuVos);
            List<MenuVo> parents = menuVos.stream()
                    .filter(x -> {
                        String menuSuperior = x.getMenuSuperior();
                        return menuSuperior != null && menuSuperior.equals("0");
                    })
                    .collect(Collectors.toList());
            List<MenuVo> list = new ArrayList<>();
            for ( MenuVo vo : parents ){
                vo.setChilds(getChild(vo.getMenuId(),menuVos));
                list.add(vo);
            }
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error("角色处于停用状态");
        }

    }

    @Override
    public AjaxResult<?> allMenuList(Map<String,Object> params) {
        if(params.containsKey("pageIndex")&&params.containsKey("pageSize")){
            int pageIndex = (int) params.get("pageIndex");
            int pageSize = (int) params.get("pageSize");
            params.put("pageIndex",(pageIndex-1) * pageSize);
            List<MenuVo> menuVo = this.menuDao.menuList(params);
            Map<String,Object> res= this.menuDao.findPageCount(params);
            res.put("menuList",menuVo);
            return AjaxResult.success(res);
        }else{
            return AjaxResult.success(this.menuDao.menuList(params));
        }
    }


    private List<MenuVo> getChild(Integer menuId, List<MenuVo> target){
        List<MenuVo> childs = new ArrayList<>();
        for(MenuVo vo : target){
            if(menuId.equals(Integer.valueOf(vo.getMenuSuperior()))){
                childs.add(vo);
            }
        }
        if ( childs.size() != 0 )
            childs.forEach( x -> x.setChilds(getChild(x.getMenuId(),target)) );
        return childs;
    }

    @Override
    public AjaxResult<?> delete(int menuId) {
        int delete = menuDao.deleteMenu(menuId);
        if(delete > 0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> update(MenuVo menuVo) {
        int update = menuDao.updateById(menuVo);
        if(update > 0){
            this.redisUtil.delKey("menuList");
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> insert(MenuVo menuVo) {
        int insert = menuDao.insert(menuVo);
        Map<String,Object> params = new HashMap<>();
        params.put("roleId",0);
        params.put("menuId",menuDao.MenuId());
        int insert1 = menuDao.addRoleMenu(params);
        if(insert > 0){
            this.redisUtil.delKey("menuList");
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }


    @Override
    public AjaxResult<?> adminMenu(int roleId) {
//        List<Integer> roleMenu = this.menuDao.findMenuId(roleId);
//        List<MenuVo> list = new ArrayList<>();
//        for(int i = 0; i < roleMenu.size(); i++) {
//            int menuId = roleMenu.get(i);
//            MenuVo menuVo = this.menuDao.roleMenuList(menuId);
//            list.add(menuVo);
//        }
        return AjaxResult.success(null);
    }

    public boolean check(String key,String key2){
        if(!this.redisUtil.containsKey(key,key2)){
            return false;
        }else{
            return true;
        }
    }
}
